Optimize TextRangeCollection.getItemContaining#11265
Open
Zaczero wants to merge 1 commit into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR optimizes TextRangeCollection.getItemContaining by replacing the generic sparse-array search with a specialized binary search tailored for dense, start-sorted TextRange arrays, aiming to reduce branching and improve hot-path performance.
Changes:
- Switched
getItemContainingto use a new dense-array search helper. - Added
getIndexContainingDensewith a tighter binary search loop and a “gap” early-out.
Comment on lines
+104
to
+106
| // Fast path for dense, start-sorted ranges: | ||
| // binary-search for the containing item, with an early-out | ||
| // when the position falls between adjacent ranges. |
Comment on lines
+111
to
+113
| while (min <= max) { | ||
| const mid = min + ((max - min) >> 1); | ||
| const item = arr[mid]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This change improved the performance on openstreetmap-ng codebase by about 1%.
14.6s → 14.4s (-1.1% ± 0.4%)
The generic getIndexContaining supports sparse arrays ((T | undefined)[]) and does extra defensive work. In our hot paths (token/line collections), the array is dense and ordered, so this version: